package com.example.service;

import com.example.mapper.PosClickMapper;
import com.example.mapper.PostMapper;
import com.example.pojo.bo.ClickBO;
import com.example.pojo.entity.PosClick;
import com.example.util.JobCollaborativeFiltering;
import com.example.util.JobRecommendUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 本文来演示
 *
 * @author 赵建云
 * @create 2023/10/13
 */
@Service
public class SavePosClickServiceImpl implements SavePosClickService{
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private PosClickMapper posClickMapper;

    @Override
    public int savePosClick(Integer jsId,Integer posId) {
        Integer count = 0;
        List<ClickBO> clickBOList = posClickMapper.findClick(jsId);
        ClickBO clickBO = postMapper.clickSave(posId);
        for (ClickBO c : clickBOList) {
            if (c.getPosKeyword().equals(clickBO.getPosKeyword())){
                count = posClickMapper.updateClick(c.getId());
            }
        }
        if (count==0){
            PosClick posClick = new PosClick();
            posClick.setJsId(jsId);
            posClick.setPosKeyword(clickBO.getPosKeyword());
            posClick.setPosType(clickBO.getPosType());
            posClick.setClickNum(1);
            posClick.setPosId(posId);
            posClick.setClickTime(new Date());
            count = posClickMapper.saveClick(posClick);
        }
        return count;
    }

    //推荐职位id
    @Override
    public List<Integer> recommendPosId(Integer userId) {
        List<PosClick> posClickList = posClickMapper.findAll();
        JobRecommendUtils jobRecommendUtils = new JobRecommendUtils();
        for (PosClick posClick : posClickList) {
            jobRecommendUtils.addRating(posClick.getJsId(),posClick.getPosId(), Float.valueOf(posClick.getClickNum()));
        }
        JobCollaborativeFiltering jobCollaborativeFiltering = new JobCollaborativeFiltering(jobRecommendUtils);
        List<Integer> posIds = jobCollaborativeFiltering.getRecommendedProducts(userId);
        return posIds;
    }
}
